import random
import time

import grpc
import hello_bibi_pb2 as pb2
import hello_bibi_pb2_grpc as pb2_grpc
def test():
    index=0
    while 1:
        time.sleep(1)
        data=str(random.random())
        if index==5:
            break
        print(index)
        index+=1
        yield pb2.TestClientSendStreamRequest(data=data)
def run():
    conn=grpc.insecure_channel('127.0.0.1:5000')
    client=pb2_grpc.BibiliStub(channel=conn)#对应客户端
    try:
        response,call=client.HelloDewei.with_call(pb2.HelloDeweiReq(
            name="dewei",
            age=33
        ),compression=grpc.Compression.Gzip,metadata=(('name','dewei'),),wait_for_ready=True)
        print(response.result)
        headers=call.trailing_metadata()
        print(headers[0].key,headers[0].value)

    except Exception as e:
        print(e.code().name,e.code().value)
        print(e.details())

    # response=client.TestClientRecvStream(pb2.TestClientRecvStreamRequest(
    #     data='dewei'
    # ))
    # for item in response:
    #     print(item.result)
    # response=client.TestClientSendStream(test())#客户端发给服务器端
    # print(response.result)
    # response=client.TestTwoWayStream(test())
 



if __name__=='__main__':
    run()
